<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/14 0014
 * Time: 18:20
 */

namespace app\admin\controller;

use think\Container;
use think\Controller;
use Session;
class Base extends Controller
{
    function __construct()
    {
        header("Cache-control: private");

        parent::__construct();

        $this->public_display();
    }

    /**
     * 初始化操作
     */
    function initialize()
    {
        //过滤不需要登陆的行为
        if (!in_array($this->request->action(), array('login', 'vertify'))) {
            if (session::get('admin_id') > 0) {
                $this->check_priv();//检查管理员菜单操作权限
            }else {
                $this->redirect('admin/system/login');
            }
        }
    }

    public function public_display() {

        //获取面包屑

    }
    public function createConfirm() {

    }

    /**
     * 验证权限
     * @return bool
     */
    public function check_priv(){
        $ctl = Container::get('request')->controller();
        $act = Container::get('request')->action();
        $act_list = session('act_list');
        //无需验证的操作
        $uneed_check = array('login', 'logout', 'vertifyHandle', 'vertify', 'imageUp', 'upload', 'videoUp', 'delupload', 'login_task');
        //var_dump($act_list);
        if ($ctl == 'Index' || $act_list == 'all') {
            //后台首页控制器无需验证,超级管理员无需验证
            return true;
        } elseif (request()->isAjax() || strpos($act, 'ajax') !== false || in_array($act, $uneed_check)) {
            //所有ajax请求不需要验证权限
            return true;
        } else {
            $right = Db('system_menu')->where("id", "in", $act_list)->cache(true)->column('right', true);
            $role_right = '';
            foreach ($right as $val) {
                $role_right .= $val . ',';
            }
            $role_right = explode(',', $role_right);
            //检查是否拥有此操作权限
            if (!in_array($ctl . '@' . $act, $role_right)) {
                $this->error('您没有操作权限[' . ($ctl . '@' . $act) . '],请联系超级管理员分配权限', url('Admin/Index/welcome'));
            }
        }
    }
}